package com.ruoyi.ice.result;

import com.ice.core.annotation.IceField;
import com.ice.core.annotation.IceNode;
import com.ice.core.context.IceRoam;
import com.ice.core.leaf.roam.BaseLeafRoamResult;
import com.ruoyi.common.utils.StringUtils;
import lombok.Data;
import lombok.EqualsAndHashCode;

import java.math.BigDecimal;

@Data
@EqualsAndHashCode(callSuper = true)
@IceNode(name = "阶梯计费", desc = "阶梯计费")
public class LadderAmount extends BaseLeafRoamResult {

    //@IceField(name = "发给谁", desc = "发放的key 如 停泊费 id")
    //private String key;
    @IceField(name = "单价", desc = "单价")
    private BigDecimal value;

    @IceField(name = "作业重量量取值", desc = "例如 berth_weight")
    private String workWeightKey;


    private BigDecimal workWeight ;

    @IceField(name = "作业天数取值(阶梯)", desc = "例如 berth_days")
    private String workDaysKey;


    private BigDecimal workDays;

    @IceField(name = "阶梯开始值(大于等于)")
    private BigDecimal minValue;

    @IceField(name = "阶梯开始值(小于)")
    private BigDecimal maxValue;

    @Override
    protected boolean doRoamResult(IceRoam roam){

        //Long uid = Long.valueOf(roam.getMulti(key));
        if (value .compareTo(BigDecimal.ZERO) ==-1 ) {
            return false;
        }

        if(StringUtils.isNotEmpty(workWeightKey)){
            Object workWeightValue = roam.getMulti(workWeightKey);

            if(StringUtils.isNotNull(workWeightValue)){
                workWeight = new BigDecimal(workWeightValue.toString());
            }else {
                workWeight = BigDecimal.ONE;
            }

            roam.put("workWeightKey",workWeightKey);

        }else {
            workWeight = BigDecimal.ONE;
        }

        if(StringUtils.isNotEmpty(workDaysKey)) {
            Object workDaysValue = roam.getMulti(workDaysKey);

            if (StringUtils.isNotNull(workDaysValue)) {
                workDays = new BigDecimal(workDaysValue.toString());
            } else {
                workDays = BigDecimal.ONE;
            }

            roam.put("workDaysKey",workDaysKey);

        } else {
            workDays = BigDecimal.ONE;
        }

        if(StringUtils.isNull(minValue)){
            minValue = BigDecimal.ZERO;
        }

        //判断 workDays 是否 在 minValue 和 maxValue 之间

        if(StringUtils.isNull(maxValue)){
            maxValue = new BigDecimal(Double.MAX_VALUE);
        }

        BigDecimal calValue = BigDecimal.ZERO;

        BigDecimal calResult = BigDecimal.ZERO;

        if(StringUtils.isNotNull(roam.get("result"))){
            calResult = new BigDecimal(roam.get("result").toString());
        }
        //如果 workDays 在 minValue 和 maxValue 之间
        if(workDays.compareTo(minValue)>-1 && workDays.compareTo(maxValue)==-1 && maxValue .compareTo(minValue) == 1 ){
            calValue = value .multiply(workWeight).multiply (workDays .subtract(minValue));
            roam.put("remark", (roam.get("remark") != null ? roam.get("remark") : "") + "单价:" + value + ";计费量:" + workDays.subtract(minValue) + ";计费结果:" + calValue + "\n");

            calResult =calResult.add(calValue);

            roam.put("result",calResult);
        }

        //如果 workDays 小于 minvalue 不计费
        if(workDays.compareTo(minValue) == -1 && workDays.compareTo(maxValue) == -1 && maxValue.compareTo(minValue)==1){
            //roam.put("result",0);
        }

        //如果 workDays 大于 maxValue
        if(workDays.compareTo(maxValue) == 1 && workDays.compareTo(minValue) == 1  && maxValue.compareTo(minValue)  == 1 ){

            calValue = value.multiply(workWeight).multiply(maxValue.subtract(minValue))  ;
            roam.put("remark",(roam.get("remark")!= null?roam.get("remark"):"")+"单价:"+value+";计费量:"+maxValue.subtract(minValue)+";计费结果:"+calValue+"\n" );

            calResult =calResult.add(calValue);

            roam.put("result",calResult);
        }
      //如果 workDays 小于 minValue
        if(workDays.compareTo(maxValue)>-1  && workDays.compareTo(minValue)>-1  && ! (maxValue.compareTo(minValue)==1)){

            calValue = value.multiply(workWeight).multiply(workDays.subtract(minValue));
            roam.put("remark",(roam.get("remark")!= null?roam.get("remark"):"")+"单价:"+value+";计费量:"+workDays.subtract(minValue)+";计费结果:"+calValue+"\n" );

            calResult =calResult.add(calValue);

            roam.put("result",calResult);
        }
        return true;

    }


}
